package com.example.crawler.component;

import com.example.crawler.entity.ExamEntity;
import com.example.crawler.mapper.ExamMapper;
import com.xiaoleilu.hutool.http.Header;
import com.xiaoleilu.hutool.http.HttpRequest;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Random;

/**
 * com.example.crawler.component
 *
 * @author 白雨浓
 * date: 17-11-27 上午10:58
 * content:
 */
@Component
public class ExamComponent {
    private static final String USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0";
    private static final String COOKIE = "UM_distinctid=15f620b167d0-0f5b9099fc89a2-72236751-100200-15f620b167e297; CNZZDATA1255363830=60405890-1509173276-null%7C1510669092; Hm_lvt_fd9bf00761167d2e9689ba3d37779881=1511668801,1511750985,1511751389,1511751461; w_upp=d867G1EB0EKdiFc8%2BrWkQ6ya7LV%2BXnIhrGjbPBLckhPT5Z1zriKmzQ-136a6fOZZAbV0gonm0zvubJ2wMPu97Z833Uvd4FFehJM%2Bprfmx5Slxljng-2a0dZUr8%2BpAAwWc3gL%2FlKLPqeJLcaifjdk2Hz%2Bo; PHPSESSID=nctfs4r3c3pjbos5c9jpdsklk0; Hm_lpvt_fd9bf00761167d2e9689ba3d37779881=1511752686";

    private static final String[] ps =
            ("北京 上海 天津 重庆 河北 山西 内蒙古 辽宁 吉林 黑龙江" +
                    " 江苏 浙江 安徽 福建 江西 山东 河南 湖北 湖南 广东" +
                    " 广西 海南 四川 贵州 云南 西藏 陕西 甘肃 青海 宁夏 新疆").split(" ");

    private static final String[] ts = "语文 数学 英语 物理 化学 生物 政治 历史 地理 文综 理综".split(" ");

    private static final String[] gs = {"高三", "高二", "高一"};

    @Autowired
    ExamMapper mapper;

    @Async
    public void start() throws InterruptedException {

        new Thread(() -> {
            handleHtml(47, "模拟");
            handleHtml(48, "高考真题");
            handleHtml(225, "状元笔记");
        }).start();

        new Thread(() -> {
            handleHtml(226, "月考");
            handleHtml(227, "期中、期末");
        }).start();

        new Thread(() -> {
            handleHtml(228, "会考、联考");
            handleHtml(229, "会考、联考");
        }).start();
    }

    private void handleHtml(int cid, String type) {
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            int index = i + 1;
            String url = "http://www.gaokaoq.com/exam/lists.html?grade=0&course=0&year=0&city=0&cid=" + cid + "&q=&p=" + index;
            String ip = "181" + "." + new Random().nextInt(254) + "." + new Random().nextInt(254) + "." + new Random().nextInt(254);
            String html = HttpRequest.get(url)
                    .header(Header.USER_AGENT, USER_AGENT)
                    .header(Header.COOKIE, COOKIE)
                    .header("X-Forwarded-For", ip)
                    .header("Cache-Control", "max-age=0")
                    .header("Upgrade-Insecure-Requests", "1")
                    .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
                    .execute().body();

            Document doc = Jsoup.parse(html);
            handleDoc(doc, type);

            Elements els = doc.select("table.com-table tr");
            if (els.size() <= 1)
                break;
        }
    }

    @Async
    public void handleDoc(Document doc, String type) {
        Elements els = doc.select("table.com-table tr");

        for (Element el : els) {
            ExamEntity entity = new ExamEntity();
            Elements tdEls = el.select("td");

            if (tdEls.size() == 0) continue;

            String name = tdEls.get(0).text();
            String year = tdEls.get(1).text().split("-")[0];
            String download = tdEls.get(3).select("a").get(1).attr("href");

            entity.setExamName(name);
            entity.setYear(year);
            entity.setType(type);

            for (String p : ps) {
                if (name.contains(p))
                    entity.setProvince(p);
            }

            for (String t : ts) {
                if (name.contains(t))
                    entity.setSubjectName(t);
            }
            if (entity.getSubjectName() == null) {
                if (name.contains("理数") || name.contains("文数"))
                    entity.setSubjectName("数学");
                if (name.contains("文科综合")) entity.setSubjectName("文综");
                if (name.contains("理科综合")) entity.setSubjectName("理综");
            }

            for (String g : gs) {
                if (name.contains(g))
                    entity.setGrade(g);
            }
            if (entity.getGrade() == null) entity.setGrade("通用");

            handleDownload(download, entity);
            System.out.println(mapper.insert(entity));
        }
    }

    public void handleDownload(String url, ExamEntity entity) {
        String path = "/home/ubuntu/临时文件夹/题目/" + entity.getYear() + "/" + entity.getType() + "/";

        File pathFile = new File(path);
        if (!pathFile.exists()) {
            boolean b = pathFile.mkdirs();
        }

        url = "http://www.gaokaoq.com" + url;
        System.out.println(url);

        String ip = "181" + "." + new Random().nextInt(254) + "." + new Random().nextInt(254) + "." + new Random().nextInt(254);
        byte[] bs = HttpRequest.get(url)
                .header(Header.USER_AGENT, USER_AGENT)
                .header(Header.COOKIE, COOKIE)
                .header("X-Forwarded-For", ip)
                .header("Cache-Control", "max-age=0")
                .header("Upgrade-Insecure-Requests", "1")
                .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
                .execute().bodyBytes();

        try {
            Files.write(Paths.get(path + entity.getExamName() + ".doc"), bs);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
